<template>
  <el-dialog title="分配权限" :visible="showDialogP" @close="hClose">
    <el-tree
      ref="tree"
      :data="list"
      node-key="id"
      :props="{label: 'name'} "
      show-checkbox
      check-strictly
      default-expand-all
    />
    <el-row>
      <el-button @click="hSave">确认</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { arr2tree } from '@/utils/index'
import { getPermission, assingPerm } from '@/api/per'
import { getpermIds } from '@/api/role'
export default {
  props: {
    showDialogP: {
      type: Boolean,
      required: true
    },
    cId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      list: []
    }
  },
  async created() {
    const res = await getPermission()
    console.log(res)
    this.list = arr2tree(res.data, 0)
  },
  methods: {
    async hSave() {
      const key = this.$refs.tree.getCheckedKeys()
      await assingPerm({ id: this.cId, permIds: key })
      this.$message.success('成功')
      this.hClose()
    },
    async get() {
      const res = await getpermIds(this.cId)
      console.log(res)
      this.$refs.tree.setCheckedKeys(res.data.permIds)
    },
    hClose() {
      this.$emit('update:showDialogP', false)
    }
  }
}
</script>

<style>

</style>
